package com.ruoyi.animal.service.impl;

import java.util.List;

import com.ruoyi.animal.domain.AnimalInformationHistory;
import com.ruoyi.animal.mapper.AnimalInformationHistoryMapper;
import com.ruoyi.animal.service.IAnimalInformationHistoryService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.animal.mapper.AnimalInformationMapper;
import com.ruoyi.animal.domain.AnimalInformation;
import com.ruoyi.animal.service.IAnimalInformationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 动物信息Service业务层处理
 * 
 * @author gw
 * @date 2025-01-06
 */
@Service
public class AnimalInformationServiceImpl implements IAnimalInformationService 
{
    @Autowired
    private AnimalInformationMapper animalInformationMapper;

    @Autowired
    private IAnimalInformationHistoryService animalInformationHistoryService;

    /**
     * 查询动物信息
     * 
     * @param animalId 动物信息主键
     * @return 动物信息
     */
    @Override
    public AnimalInformation selectAnimalInformationByAnimalId(Long animalId)
    {
        return animalInformationMapper.selectAnimalInformationByAnimalId(animalId);
    }

    /**
     * 查询动物信息列表
     * 
     * @param animalInformation 动物信息
     * @return 动物信息
     */
    @Override
    public List<AnimalInformation> selectAnimalInformationList(AnimalInformation animalInformation)
    {
        return animalInformationMapper.selectAnimalInformationList(animalInformation);
    }

    /**
     * 新增动物信息
     * 
     * @param animalInformation 动物信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAnimalInformation(AnimalInformation animalInformation)
    {
        animalInformation.setCreateTime(DateUtils.getNowDate());
        animalInformation.setCreateBy(SecurityUtils.getUsername());
        int result = animalInformationMapper.insertAnimalInformation(animalInformation);
        AnimalInformationHistory animalInformationHistory = new AnimalInformationHistory();
        BeanUtils.copyProperties(animalInformation,animalInformationHistory);
        animalInformationHistory.setOperationType("add");
        animalInformationHistoryService.insertAnimalInformationHistory(animalInformationHistory);
        return result;
    }

    /**
     * 修改动物信息
     * 
     * @param animalInformation 动物信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAnimalInformation(AnimalInformation animalInformation)
    {
        animalInformation.setUpdateTime(DateUtils.getNowDate());
        animalInformation.setUpdateBy(SecurityUtils.getUsername());
        AnimalInformationHistory animalInformationHistory = new AnimalInformationHistory();
        BeanUtils.copyProperties(animalInformation,animalInformationHistory);
        animalInformationHistory.setOperationType("update");
        animalInformationHistoryService.insertAnimalInformationHistory(animalInformationHistory);
        return animalInformationMapper.updateAnimalInformation(animalInformation);
    }

    /**
     * 批量删除动物信息
     * 
     * @param animalIds 需要删除的动物信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAnimalInformationByAnimalIds(Long[] animalIds)
    {
        int deletedCount = 0;
        for (Long animalId : animalIds) {
            AnimalInformation animalInformation = animalInformationMapper.selectAnimalInformationByAnimalId(animalId);
            if(animalInformation != null){
                animalInformation.setDeleteBy(SecurityUtils.getUsername());
                animalInformation.setDeleteTime(DateUtils.getNowDate());
                animalInformation.setDelFlag(1);
                animalInformationMapper.updateAnimalInformation(animalInformation);
                deletedCount++;
            }
            AnimalInformationHistory animalInformationHistory = new AnimalInformationHistory();
            BeanUtils.copyProperties(animalInformationMapper.selectAnimalInformationByAnimalId(animalId),animalInformationHistory );
            animalInformationHistory.setOperationType("delete");
            animalInformationHistoryService.insertAnimalInformationHistory(animalInformationHistory);
        }
        return deletedCount;
    }

    /**
     * 删除动物信息信息
     * 
     * @param animalId 动物信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAnimalInformationByAnimalId(Long animalId)
    {
        AnimalInformation animalInformation = animalInformationMapper.selectAnimalInformationByAnimalId(animalId);
        animalInformation.setDeleteBy(SecurityUtils.getUsername());
        animalInformation.setDeleteTime(DateUtils.getNowDate());
        animalInformation.setDelFlag(1);
        AnimalInformationHistory animalInformationHistory = new AnimalInformationHistory();
        BeanUtils.copyProperties(animalInformationMapper.selectAnimalInformationByAnimalId(animalId),animalInformationHistory );
        animalInformationHistory.setOperationType("delete");
        animalInformationHistoryService.insertAnimalInformationHistory(animalInformationHistory);
        return animalInformationMapper.updateAnimalInformation(animalInformation);
    }
}
